package com.iwunu.gateway.controller;

import com.iwunu.gateway.constant.ResultCodeEnum;
import com.iwunu.gateway.entity.InspectRecords;
import com.iwunu.gateway.service.InspectRecodesService;
import com.iwunu.gateway.vo.InspectRecordsVo;
import com.iwunu.iot.common.util.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author kinfeng
 * @date 2020/4/28 11:22 AM
 * @description
 */
@CrossOrigin
@RequestMapping("/product")
@Controller
@Api(tags = "生产数据", description = "生产数据")
public class InspectController {
    @Autowired
    private InspectRecodesService inspectRecodesService;



	/**
	 ** 保存检查结果
	 * 
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/inspect", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Result> save(
			@RequestBody InspectRecordsVo inspectRecordsVo) {
		Result result = null;
		if(inspectRecordsVo.getDeviceId()==null) {
			result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(), null);
			return new ResponseEntity<>(result, HttpStatus.MULTIPLE_CHOICES);
		}
		InspectRecords ir  = inspectRecodesService.selectRecodes(inspectRecordsVo.getDeviceId());
		if(ir!=null && ir.getId()!=null) {
			ir = inspectRecodesService.update(ir);
			result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(), null);	
			return new ResponseEntity<>(result, HttpStatus.OK);
		}
		ir = inspectRecodesService.save(inspectRecordsVo);
		if(ir!=null && ir.getId()!=null) {
			result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(), null);	
		}else {
			result = new Result(ResultCodeEnum.DATA_ERROR.getCode(), ResultCodeEnum.DATA_ERROR.getDesc(), null);
		}
		return new ResponseEntity<>(result, HttpStatus.OK);
	}
	/**
	 ** 检查结果
	 * 
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/verify", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<Result> verify(
			@ApiParam(value = "设备ID", required = true) @RequestParam("mac") String mac
			) {
		Result result = null;
		if(StringUtils.isBlank(mac)) {
			result = new Result(ResultCodeEnum.INVALID_PARAMS.getCode(), ResultCodeEnum.INVALID_PARAMS.getDesc(), null);
			return new ResponseEntity<>(result, HttpStatus.MULTIPLE_CHOICES);
		}
		InspectRecords ir  = inspectRecodesService.selectRecodes(mac);
		if(ir!=null ) {
			result = new Result(ResultCodeEnum.OK.getCode(), ResultCodeEnum.OK.getDesc(), null);	
			return new ResponseEntity<>(result, HttpStatus.OK);
		}
		
		 result = new Result(ResultCodeEnum.DATA_ERROR.getCode(), ResultCodeEnum.DATA_ERROR.getDesc(), null);
		 return new ResponseEntity<>(result, HttpStatus.MULTIPLE_CHOICES);
	}
}
